/**
 * 生成牌
 * 洗牌
 * 发牌
 * 留底牌
 * 排序 按 number 排，number 相同，按花色排
 */

//随机数
function random(min, max) {

    if (!max) {
        // 换
        [max, min] = [min, 0];
    }
    let number = parseInt(Math.random() * (max - min + 1) + min);
    return number;
}
//'♠️3'
const number = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2'];
const flower = ['♠️', '♥️', '♣️', '♦️']; // 黑桃  红心  梅花  方块
let cards = [{ number: 14, flower: '小王', text: '小王' }, { number: 15, flower: '大王', text: '大王' }];

//生成牌
function bring() {
	for (let f = 0; f < flower.length; f++) {
		for (let n = 0; n < number.length; n++) {
			cards.push({
				number: n,//用下标做为值方便后面排序
				flower: f,
				text: flower[f] + number[n]//打印给玩家看到的牌型
			})
		}
	}
	console.log(cards);
}
bring();

// 洗牌
function shuffle() {
	for (let i = 0; i < cards.length; i++) {
		let num = random(1, 53);//生成随机数
		[cards[i], cards[num]] = [cards[num], cards[i]];
		//下标为随机数的项与遍历到的项交换
	}

	console.log(cards);
}
shuffle()

//发牌
function deal() {
	let first = cards.slice(0, 17);
	let second = cards.slice(17, 34);
	let third = cards.slice(34, 51);
	let others = cards.slice(51)

	first = mySort(first);
	second = mySort(second);
	third = mySort(third);
	others = mySort(others);

	console.log('玩家一', first, '玩家二', second, '玩家三', third, '底牌', others);
}
deal()

//排序
function mySort(player) {
	player.sort((a, b) => a.number == b.number ? a.flower - b.flower : a.number - b.number)
	//先按number排序，number相同按花色排序
	let cards = player.map(item => item.text);
	return cards;
}






















